<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Reversing an Animation</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Reversing an Animation</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><link href="../assets/anim/anim.css" rel="stylesheet" type="text/css">
<div class="intro">
    <p>This demonstrates how to use the <code>reverse</code> attribute to change the behavior of the animation.</p>
    <p> Click the icon in the header to toggle the element's <code>height</code>.</p>
</div>

<div class="example">
<div id="demo" class="yui3-module">
    <div class="yui3-hd">
        <h3>Animation Demo</h3>
    </div>
    <div class="yui3-bd">
        <p>This an example of what you can do with the YUI Animation Utility.</p>
        <p><em>Follow the instructions above to see the animation in action.</em></p>
    </div>
</div>
<p>This is placeholder text used to demonstrate how the above animation affects subsequent content.</p> 

<script type="text/javascript">
YUI().use('anim', function(Y) {
    var module = Y.one('#demo');

    // add fx plugin to module body
    var content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, {
        from: { height: 1 },
        to: {
            height: function(node) { // dynamic in case of change
                return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
            }
        },

        easing: Y.Easing.easeOut,
        duration: 0.5
    });

    var onClick = function(e) {
        module.toggleClass('yui3-closed');
        content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse 
        content.fx.run();
    };

    // use dynamic control for dynamic behavior
    var control = Y.Node.create(
        '<a title="show/hide content" class="yui3-toggle">' +
            '<em>toggle</em>' +
        '</a>'
    );

    // append dynamic control to header section
    module.one('.yui3-hd').appendChild(control);
    control.on('click', onClick);

});
</script>

</div>

<h2>Setting up the HTML</h2>
<p>First we add some HTML to animate.</p>

<pre class="code prettyprint">&lt;div id=&quot;demo&quot; class=&quot;yui3-module&quot;&gt;
    &lt;div class=&quot;yui3-hd&quot;&gt;
        &lt;h3&gt;Animation Demo&lt;&#x2F;h3&gt;
        &lt;a title=&quot;remove module&quot; class=&quot;yui3-toggle&quot;&gt;&lt;em&gt;-&lt;&#x2F;em&gt;&lt;&#x2F;a&gt;
    &lt;&#x2F;div&gt;
    &lt;div class=&quot;yui3-bd&quot;&gt;
        &lt;p&gt;This an example of what you can do with the YUI Animation Utility.&lt;&#x2F;p&gt;
        &lt;p&gt;&lt;em&gt;Follow the instructions above to see the animation in action.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;</pre>


<h2>Using the NodeFX Plugin</h2>
<p>For this example, we will use <code>Node</code>'s <code>fx</code> plugin to animate the element.  The plugin adds the anim instance to the <code>Node</code> instance, pre-configuring it to use the Node instance as the <code>Anim</code>'s node.  The <code>plug</code> method accepts a class to construct and an optional config to pass to the constructor.</p>
<p>Setting the <code>from</code> attribute to the expanded height of the element allows us to toggle the effect using the <code>reverse</code> attribute, which we will see below (<code>from</code> uses current value when omitted).</p>

<pre class="code prettyprint">var module = Y.one(&#x27;#demo&#x27;);

&#x2F;&#x2F; add fx plugin to module body
var content = module.one(&#x27;.yui3-bd&#x27;).plug(Y.Plugin.NodeFX, {
    from: { height: 0 },
    to: {
        height: function(node) { &#x2F;&#x2F; dynamic in case of change
            return node.get(&#x27;scrollHeight&#x27;); &#x2F;&#x2F; get expanded height (offsetHeight may be zero)
        }
    },

    easing: Y.Easing.easeOut,
    from: { height: 0 },
    duration: 0.5
});</pre>


<h2>Creating the Control Element</h2>
<p>Because our behavior only works when JS is available, let's go ahead and add our control element using JS as well.</p>

<pre class="code prettyprint">&#x2F;&#x2F; use dynamic control for dynamic behavior
var control = Y.Node.create(
    &#x27;&lt;a title=&quot;show&#x2F;hide content&quot; class=&quot;yui3-toggle&quot;&gt;&#x27; +
        &#x27;&lt;em&gt;toggle&lt;&#x2F;em&gt;&#x27; +
    &#x27;&lt;&#x2F;a&gt;&#x27;
);

&#x2F;&#x2F; append dynamic control to header section
module.one(&#x27;.yui3-hd&#x27;).appendChild(control);</pre>


<h2>Toggling Animation Behavior</h2>
<p>Before calling <code>run</code> in our <code>click</code> handler, we will use the <code>reverse</code> attribute toggle the direction of the animation depending on whether its opening or closing.</p>

<pre class="code prettyprint">var onClick = function(e) {
    module.toggleClass(&#x27;yui-closed&#x27;);
    content.fx.set(&#x27;reverse&#x27;, !content.fx.get(&#x27;reverse&#x27;)); &#x2F;&#x2F; toggle reverse
};</pre>


<h2>Running the Animation</h2>
<p>Finally we add an event handler to run the animation.</p>

<pre class="code prettyprint">module.one(&#x27;.yui3-toggle&#x27;).on(&#x27;click&#x27;, onClick);</pre>


<h2>Complete Example Source</h2>
<pre class="code prettyprint">&lt;div id=&quot;demo&quot; class=&quot;yui3-module&quot;&gt;
    &lt;div class=&quot;yui3-hd&quot;&gt;
        &lt;h3&gt;Animation Demo&lt;&#x2F;h3&gt;
    &lt;&#x2F;div&gt;
    &lt;div class=&quot;yui3-bd&quot;&gt;
        &lt;p&gt;This an example of what you can do with the YUI Animation Utility.&lt;&#x2F;p&gt;
        &lt;p&gt;&lt;em&gt;Follow the instructions above to see the animation in action.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;This is placeholder text used to demonstrate how the above animation affects subsequent content.&lt;&#x2F;p&gt; 

&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
YUI().use(&#x27;anim&#x27;, function(Y) {
    var module = Y.one(&#x27;#demo&#x27;);

    &#x2F;&#x2F; add fx plugin to module body
    var content = module.one(&#x27;.yui3-bd&#x27;).plug(Y.Plugin.NodeFX, {
        from: { height: 1 },
        to: {
            height: function(node) { &#x2F;&#x2F; dynamic in case of change
                return node.get(&#x27;scrollHeight&#x27;); &#x2F;&#x2F; get expanded height (offsetHeight may be zero)
            }
        },

        easing: Y.Easing.easeOut,
        duration: 0.5
    });

    var onClick = function(e) {
        module.toggleClass(&#x27;yui3-closed&#x27;);
        content.fx.set(&#x27;reverse&#x27;, !content.fx.get(&#x27;reverse&#x27;)); &#x2F;&#x2F; toggle reverse 
        content.fx.run();
    };

    &#x2F;&#x2F; use dynamic control for dynamic behavior
    var control = Y.Node.create(
        &#x27;&lt;a title=&quot;show&#x2F;hide content&quot; class=&quot;yui3-toggle&quot;&gt;&#x27; +
            &#x27;&lt;em&gt;toggle&lt;&#x2F;em&gt;&#x27; +
        &#x27;&lt;&#x2F;a&gt;&#x27;
    );

    &#x2F;&#x2F; append dynamic control to header section
    module.one(&#x27;.yui3-hd&#x27;).appendChild(control);
    control.on(&#x27;click&#x27;, onClick);

});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Creating and using a simple animation.">
                                        <a href="basic.html">Basic Animation</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The Animation Utility allows you to implement &#x27;easing effects&#x27; &mdash; for example, when an animation gradually slows down as it nears completion, that&#x27;s an easing effect known as &#x27;ease-in&#x27;.  This example shows you how to use easing effects with your animations.">
                                        <a href="easing.html">Easing Effects</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Color animations can be effective indicators of state during the lifespan of a dynamic page.  This example shows you how to animate color attributes of an element.">
                                        <a href="colors.html">Animating Colors</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The direction attribute can be used to reverse the animation on alternate iterations.">
                                        <a href="alt-iterations.html">Alternating Iterations</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example shows you how to animate the xy coordinates of an element.">
                                        <a href="anim-xy.html">Animating XY Position</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example demonstrates animating an element along a curved path using bezier control points.">
                                        <a href="curve.html">Animating Along a Curved Path</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The reverse attribute allows you to change the run direction of an animation.">
                                        <a href="reverse.html">Reversing an Animation</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example demonstrates how to use the end event.">
                                        <a href="end-event.html">Using the End Event</a>
                                    </li>
                                
                            
                                
                            
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="Shows how to create a simple plugin to animate the Overlay&#x27;s movement and visibility.">
                                        <a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Working with multiple YUI instances.">
                                        <a href="../yui/yui-multi.html">Multiple Instances</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to make an animated node a Drop target.">
                                        <a href="../dd/anim-drop.html">Animated Drop Targets</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
